package org.tea.inventory.admin.web.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.tea.inventory.admin.util.PageUtils;
import org.tea.inventory.base.page.PageDomain;
import org.tea.inventory.base.page.TableDataInfo;
import org.tea.inventory.base.page.TableSupport;
import org.tea.inventory.base.response.BaseResponse;
import org.tea.inventory.base.util.DateUtils;
import org.tea.inventory.base.util.ResponseUtil;
import org.tea.inventory.base.util.StringUtils;
import org.tea.inventory.base.util.sql.SqlUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.beans.PropertyEditorSupport;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * web层通用数据处理
 * 
 * @author ruoyi
 */
public class BaseController
{
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 将前台传递过来的日期格式的字符串，自动转化为Date类型
     */
    @InitBinder
    public void initBinder(WebDataBinder binder)
    {
        // Date 类型转换
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport()
        {
            @Override
            public void setAsText(String text)
            {
                setValue(DateUtils.parseDate(text));
            }
        });
    }

    /**
     * 设置请求分页数据
     */
    protected void startPage()
    {
        PageUtils.startPage();
    }

    /**
     * 设置请求排序数据
     */
    protected void startOrderBy()
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        if (StringUtils.isNotEmpty(pageDomain.getOrderBy()))
        {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.orderBy(orderBy);
        }
    }

    /**
     * 清理分页的线程变量
     */
    protected void clearPage()
    {
        PageUtils.clearPage();
    }


    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    protected BaseResponse getDataTable(List<?> list)
    {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setItems(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return ResponseUtil.ok(rspData);
    }
    /*购买积分时使用*/
    protected BaseResponse getDataTableIntegral(List<?> list,Integer rate)
    {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setItems(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        rspData.setRate(rate);
        return ResponseUtil.ok(rspData);
    }
    //有分页条件时使用
    protected BaseResponse getDataTablePage(List<?> list, int page, int limit)
    {
        if (list == null) {
            list = new ArrayList<>();
        }

        // 计算总记录数
        int totalRecords = list.size();

        // 计算总页数
        int totalPages = (int) Math.ceil((double) totalRecords / limit);

        // 处理页码越界情况
        if (page < 1) {
            page = 1;
        }
        if (page > totalPages && totalPages > 0) {
            page = totalPages;
        }

        // 计算起始索引和结束索引
        int startIndex = (page - 1) * limit;
        int endIndex = Math.min(startIndex + limit, totalRecords);

        // 截取当前页数据
        List<?> pageData;
        if (startIndex >= totalRecords) {
            pageData = new ArrayList<>();
        } else {
            pageData= list.subList(startIndex, endIndex);
        }
        TableDataInfo rspData = new TableDataInfo();
        rspData.setItems(pageData);
        rspData.setTotal(new PageInfo(list).getTotal());
        return ResponseUtil.ok(rspData);
    }
}
